//    Copyright (C) Kherty.  All rights reserved.
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using OpenLS.Spreadsheet.UI.Internals.Resources;

namespace OpenLS.Spreadsheet.UI.Internals
{
    partial class FormulaBarResources
    {
        public FormulaBarResources()
        {
            InitializeComponent();
        }
        void onCancel(object sender, RoutedEventArgs e)
        {
            EditionElement.SetEditedCellFor(sender, null);

        }
        void onEnter(object sender, RoutedEventArgs e)
        {
            var ws = (Worksheet)((FrameworkElement)sender).DataContext;
            EditedCell c = EditionElement.GetEditedCell((DependencyObject)sender);
            if (c.HasError)
            {
                MessageBox.Show(UIStringTable.FormulaHasErrors); 
                return;
            }
            ws.ActiveCell.Formula = c.Text;
            EditionElement.SetEditedCellFor(sender, null);
        }
        void onInsertFunction(object sender, RoutedEventArgs executedRoutedEventArgs)
        {
            GridFormulaEditing.InsertFunction(sender, null);
        }
        void onFormulaKeyDown(object sender, KeyEventArgs args)
        {
            if (args.Key == Key.Enter)
            {
                string text = ((TextBox)sender).Text;
                var currentWorksheet = ((Worksheet)((FrameworkElement)sender).DataContext);
                currentWorksheet.ActiveCell.Formula = text;
                if (currentWorksheet.Selection == null || currentWorksheet.Selection.RowSpan * currentWorksheet.Selection.ColumnSpan == 1)
                {
                    currentWorksheet.ActiveCell = currentWorksheet.ActiveCell.GetNeighbor(NavigationDirection.Down);
                    currentWorksheet.Selection = new SingleCellRange(currentWorksheet, currentWorksheet.ActiveCell.Address);
                }
                else
                {
                    currentWorksheet.ActiveCell = currentWorksheet.Selection.GetNextCell(currentWorksheet.ActiveCell, CellNavigationDirection.Forward);
                }
                args.Handled = true;
                var e = (DependencyObject)Keyboard.FocusedElement as FrameworkElement;
                if (e == null)
                    return;
                e.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
            }
        }

        void onFormulaMouseDown(object sender, MouseButtonEventArgs args)
        {
            var element = (FrameworkElement)sender;
            var ws = (Worksheet)element.DataContext;
            if (!ws.ActiveCell.CanBeEdited)
            {
                MessageBox.Show(UIStringTable.Protected);
                args.Handled = true;
                return;
            }
            WorksheetViewModel worksheetViewModel = WorksheetHelper.GetWorksheetViewModel(element);
            EditionElement.SetEditedCellFor(sender, new EditedCell(worksheetViewModel, ws.ActiveCell, GridEditing.GetActiveText(ws), EditionMode.Edit));

            var c = (ContentControl)element.Tag;
            var cp = VisualTreeHelper.GetChild(c, 0) as ContentPresenter;
            var tb = (CellTextBox)cp.ContentTemplate.FindName("editedCellTB", cp);
            tb.Focus();
            var a = new MouseButtonEventArgs(args.MouseDevice, args.Timestamp, MouseButton.Left) {RoutedEvent = TextBox.MouseDownEvent};
            tb.RaiseEvent(a);
            args.Handled = true;
        }
        void onAlways(object sender, CanExecuteRoutedEventArgs args)
        {
            args.CanExecute = true;
            args.Handled = true;
        }

        


    }
}
